<template>
  <div class="app-container">
    <div>当前任务：{{ job.name }}</div>
    <el-divider/>
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="表名" prop="username">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          @change="handleQuery"
          clearable
          style="width: 240px"
        >
          <dictionary-option code="jobStatus"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="Refresh" @click="handleResetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="dataList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column
        label="表名"
        prop="tableName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" prop="status" >
        <template #default="{ row }">
          <dictionary-tag
            code="jobStatus"
            :value="row.status"
          ></dictionary-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="行数"
        prop="dataNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="耗时"
        align="center"
        prop="costTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="上次同步时间"
        align="center"
        prop="lastSyncTime"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="同步开始时间"
        align="center"
        prop="beginTime"
        :show-overflow-tooltip="true"
      />
    </el-table>

    <pagination
      v-show="totalCount > 0"
      :total="totalCount"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getPage"
    />
  </div>
</template>
<script setup>
import {getCurrentInstance, ref} from 'vue'
import {useRouter} from 'vue-router'

import {getById, tableLogPage} from '@/api/sync/job'

const router = useRouter()

const {proxy} = getCurrentInstance()

const queryParams = ref({
  current: 1,
  size: 10,
  tableName: undefined,
  status: undefined,
  triggerId: router.currentRoute.value.params.id
})

const loading = ref(true)
const dataList = ref([])
const totalCount = ref(0)
const ids = ref([])
const multiple = ref(true)
const job = ref({})

/**
 * 搜索
 */
function handleQuery() {
  getPage()
}

/**
 * 重置
 */
function handleResetQuery() {
  proxy.resetForm('queryFormRef')
  handleQuery()
}

/**
 * 获取数据
 */
async function getPage() {
  loading.value = true
  const {
    data: {total, list}
  } = await tableLogPage(queryParams.value)
  totalCount.value = parseInt(total)
  dataList.value = list
  loading.value = false
}

/**
 * 获取角色信息
 */
async function getJob() {
  const {data} = await getById(queryParams.value.triggerId)
  job.value = data
}

/**
 * 选择条数
 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  multiple.value = !selection.length
}

/**
 * 初始化
 */
getPage()
getJob()
</script>
